package com.dinadi.yidi.ui.view;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.dinadi.yidi.R;

/**
 * author: JiaYu.He
 * Date: 2016-04-21
 * Time: 09:48
 */
/** 分享窗口 */
public class ShareMenuPopupWindow extends PopupWindow implements OnClickListener {

	private Activity mContext;
	private View mMenuView;
	private ViewFlipper viewfipper;
	private RelativeLayout  weixin, friends, btn_cancel;

	public ShareMenuPopupWindow(Activity context) {
		super(context);
		mContext = context;
		mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//多加这一句，问题就解决了！这句的官方文档解释是：让窗口背景后面的任何东西变暗  
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.popupwindow_share, null);
		viewfipper = new ViewFlipper(context);
		viewfipper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	//	qq = (RelativeLayout) mMenuView.findViewById(R.id.qq);
		weixin = (RelativeLayout) mMenuView.findViewById(R.id.weixin);
		friends = (RelativeLayout) mMenuView.findViewById(R.id.friends);
		//weibo = (RelativeLayout) mMenuView.findViewById(R.id.weibo);
		btn_cancel = (RelativeLayout) mMenuView.findViewById(R.id.cancel);

		//qq.setOnClickListener(this);
		weixin.setOnClickListener(this);
		friends.setOnClickListener(this);
		//weibo.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);



		viewfipper.addView(mMenuView);
		viewfipper.setFlipInterval(6000000);
		this.setContentView(viewfipper);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.dialogstyle);
		ColorDrawable dw = new ColorDrawable(0x20000000);
		this.setBackgroundDrawable(dw);
		this.update();

	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		viewfipper.startFlipping();
		WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
		lp.alpha = 0.3f;
		mContext.getWindow().setAttributes(lp);
		setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
				lp.alpha = 1f;
				mContext.getWindow().setAttributes(lp);
			}
		});
	}

	public void onClick(View v) {
		if (listener != null) {
			switch (v.getId()) {
//			case R.id.qq:
//				listener.onShareClick(0);
//				break;
			case R.id.weixin:
				listener.onShareClick(0);
				break;
			case R.id.friends:
				listener.onShareClick(1);
				break;
//			case R.id.weibo:
//				listener.onShareClick(3);
//				break;
			case R.id.cancel:
				 dismiss();
				break;
			default:
				break;
			}
		}
	}

	private OnShareClickListener listener;

	public interface OnShareClickListener {
		void onShareClick(int position);
	}

	public void setOnShareClickListener(OnShareClickListener listener) {
		this.listener = listener;
	}

}
